using System;
using System.Windows;
using System.Windows.Input;
using Common;
using ProjectView.Windows;
using SProject;

namespace ProjectView.Permission
{
	public class ProjectManageWindowPermission : ProjectManageWindow
	{
		public event DelegateOnPermissionLogEvent OnPermissionLog;

		public event DelegateOnPermissionLogCallbackEvent OnPermissionLogCallback;

		public ProjectManageWindowPermission()
		{
			base.Loaded += ProjectManageWindowPermission_Loaded;
			base.OnProjectSave += ProjectManageWindowPermission_OnProjectSave;
			foreach (CommandBinding commandBinding in base.CommandBindings)
			{
				commandBinding.Executed += CommandBinding_Executed;
			}
		}

		private void ProjectManageWindowPermission_OnProjectSave(object sender, string actionName, Action callback)
		{
			Project project = ((ProjectModifyMainWindow)sender).Project;
			if (project != null && this.OnPermissionLogCallback != null)
			{
				string name = project.BasicInfo.Name;
				this.OnPermissionLogCallback(sender, new PermissionLogEventArgs
				{
					LogType = 1,
					Action = actionName,
					ActionTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
					Content = name,
					WindowName = "ProjectModifyMainWindow"
				}, callback);
			}
		}

		private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			RoutedUICommand routedUICommand = e.Command as RoutedUICommand;
			if (routedUICommand == null || routedUICommand.Text.Equals("New") || this.OnPermissionLog == null)
			{
				return;
			}
			string content = string.Empty;
			if (routedUICommand.Text.Equals(Utility.GetMessage("Project_Memu_Edit_Edit")) || routedUICommand.Text.Equals(Utility.GetMessage("Project_Memu_Edit_Rename")) || routedUICommand.Text.Equals(Utility.GetMessage("Project_Memu_Edit_Delete")))
			{
				Project project = gridProjectNameList.SelectedItem as Project;
				if (project != null)
				{
					content = project.BasicInfo.Name;
				}
			}
			this.OnPermissionLog(sender, new PermissionLogEventArgs
			{
				Action = routedUICommand.Text,
				ActionTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
				Content = content
			});
		}

		private void ProjectManageWindowPermission_Loaded(object sender, RoutedEventArgs e)
		{
			PermissionRefresh();
		}

		public void PermissionRefresh()
		{
			if (!PermissionControl.GetInstance().UserPermissions.XMGL.XMGL01)
			{
				btnNew.Visibility = Visibility.Collapsed;
				btnEdit.Visibility = Visibility.Collapsed;
			}
			else
			{
				btnNew.Visibility = Visibility.Visible;
				btnEdit.Visibility = Visibility.Visible;
			}
			if (!PermissionControl.GetInstance().UserPermissions.XMGL.XMGL02)
			{
				btnDelete.Visibility = Visibility.Collapsed;
			}
			else
			{
				btnDelete.Visibility = Visibility.Visible;
			}
			if (!PermissionControl.GetInstance().UserPermissions.XMGL.XMGL03)
			{
				btnImport.Visibility = Visibility.Collapsed;
			}
			else
			{
				btnImport.Visibility = Visibility.Visible;
			}
			if (!PermissionControl.GetInstance().UserPermissions.XMGL.XMGL04)
			{
				btnExport.Visibility = Visibility.Collapsed;
			}
			else
			{
				btnExport.Visibility = Visibility.Visible;
			}
		}
	}
}
